package com.atguigu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.mapper.BaseCategoryViewMapper;
import com.atguigu.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author zhangqiang
 * @since 2021-03-27
 */
@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {
    @Override
    public List<JSONObject> getIndexCategoryInfo() {
        List<JSONObject> jsonObjectList = new ArrayList<>();

        List<BaseCategoryView> baseCategoryViewList = baseMapper.selectList(null);
        //获取到全部
        Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //获取一级分类
        int index = 1;
        for (Map.Entry<Long, List<BaseCategoryView>> category1Set : category1Map.entrySet()) {
            List<BaseCategoryView> baseCategoryView1List = category1Set.getValue();
            Long baseCategoryView1Id = category1Set.getKey();
            JSONObject category1Json = new JSONObject();
            category1Json.put("index",index++);//index
            category1Json.put("categoryId",baseCategoryView1Id);//ID
            category1Json.put("categoryName",baseCategoryView1List.get(0).getCategory1Name());//Name
            Map<Long, List<BaseCategoryView>> category2Map = baseCategoryView1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //获取一级分类下的二级分类
            List<JSONObject> jsonObjectList2 = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> category2Set : category2Map.entrySet()) {
                JSONObject category2Json = new JSONObject();
                List<BaseCategoryView> baseCategoryView2List = category2Set.getValue();
                Long baseCategoryView2Id = category2Set.getKey();
                category2Json.put("categoryId",baseCategoryView2Id);//ID
                category2Json.put("categoryName",baseCategoryView2List.get(0).getCategory2Name());//Name
                //获取二级分类下的三级分类
                List<JSONObject> jsonObjectList3 = new ArrayList<>();
                for (BaseCategoryView baseCategory3View : baseCategoryView2List) {
                    JSONObject category3Json = new JSONObject();
                    category3Json.put("categoryId",baseCategory3View.getCategory3Id());//ID
                    category3Json.put("categoryName",baseCategory3View.getCategory3Name());//Name
                    jsonObjectList3.add(category3Json);//添加json
                }
                category2Json.put("categoryChild",jsonObjectList3);//Child
                jsonObjectList2.add(category2Json);//添加json
            }

            category1Json.put("categoryChild",jsonObjectList2);//Child
            jsonObjectList.add(category1Json);//添加json
        }
        return jsonObjectList;
    }
}
